package com.v1.video.activity;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ksyun.media.player.IMediaPlayer;
import com.ksyun.media.player.KSYMediaPlayer;
import com.v1.video.R;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;
import com.v1.video.util.QosObject;
import com.v1.video.util.Strings;
import com.v1.video.view.VideoSurfaceView;


/**
 * 
 */
public class VideoPlayerActivityJin extends Activity{

    private static final String TAG = "VideoPlayerActivityJin";

    public static final int UPDATE_SEEKBAR = 0;
    public static final int HIDDEN_SEEKBAR = 1;
    public static final int UPDATE_QOS  = 2;

    private Context mContext;
    private KSYMediaPlayer ksyMediaPlayer;
//    private QosThread mQosThread;

    private Surface mSurface = null;
    private VideoSurfaceView mVideoSurfaceView = null;
    private SurfaceHolder mSurfaceHolder = null;

    private Handler mHandler;

    // UI
    private LinearLayout mPlayerPanel;
    private ImageView mPlayerStartBtn;
    private SeekBar mPlayerSeekbar;
    private TextView mPlayerPosition;
    private TextView mLoadText;
//    private TextView mCpu;
//    private TextView mMemInfo;
//    private TextView mVideoResolution;
//    private TextView mVideoBitrate;
//    private TextView mFrameRate;
//    private TextView mCodecType;
//    private TextView mServerIp;
    private View mPlayerBtnReverse;
    
    private RelativeLayout layoutPlayer_qos = null;

    private boolean mPlayerPanelShow = false;
    private boolean mPause = false;

    private long mStartTime = 0;
    private long mPauseStartTime = 0;
    private long mPausedTime = 0;

    private int mVideoWidth = 0;
    private int mVideoHeight = 0;
    private int screenWidth;
    private int screenHeight;
    
    String videoUrl = "";
    /**
	 * 设置视频播放屏幕大小的模式
	 * 
	 * @param flag
	 */
	private void setVideoDefauleScale() {//
		int mWidth = screenWidth;
//		double ar = (double) videoWidth / (double) videoHeight;
		int mHeight = (int) (mWidth * 9 / 16);
		Logger.i(TAG, "mWidth==="+mWidth);
		Logger.i(TAG, "mHeight==="+mHeight);
		mVideoSurfaceView.setVideoDimension(mWidth,mHeight);
		mVideoSurfaceView.requestLayout();
	}
	/**
	 * 设置视频播放横屏
	 * 
	 * @param flag
	 */
	private void setVideoDefauleScaleLandScape() {// ft_9.16方法修改，
		
//		int videoWidth = vv_video.getVideoWidth();
//		int videoHeight = vv_video.getVideoHeight();
		int mWidth = screenHeight;
//		double ar = (double) videoWidth / (double) videoHeight;
		int mHeight = screenWidth;
		mVideoSurfaceView.setVideoDimension(mWidth,mHeight);
		mVideoSurfaceView.requestLayout();
		View decorView = getWindow().getDecorView();
		// Hide both the navigation bar and the status bar.  
		// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as  
		// a general rule, you should design your app to hide the status bar whenever you  
		// hide the navigation bar.  
//		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  
//		              | View.SYSTEM_UI_FLAG_FULLSCREEN;
		int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
//		int uiOptions = View.INVISIBLE;  
		decorView.setSystemUiVisibility(uiOptions);  
	}
    private IMediaPlayer.OnPreparedListener mOnPreparedListener = new IMediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(IMediaPlayer mp) {
            if (ksyMediaPlayer != null) {
                mVideoWidth = ksyMediaPlayer.getVideoWidth();
                mVideoHeight = ksyMediaPlayer.getVideoHeight();
                Logger.i(TAG, "mVideoWidth==="+mVideoWidth+"_mVideoHeight=="+mVideoHeight);
                if(mVideoSurfaceView != null)
                {
                	setVideoDefauleScale();
                	mVideoSurfaceView.invalidate();
//                    mVideoSurfaceView.setVideoDimension(ksyMediaPlayer.getVideoWidth(), ksyMediaPlayer.getVideoHeight());
//                    mVideoSurfaceView.requestLayout();
                	
                }

                ksyMediaPlayer.start();
            }
            setVideoProgress(0);
//            if (mQosThread != null)
//                mQosThread.start();
//            mCpu.setVisibility(View.VISIBLE);
//            mMemInfo.setVisibility(View.VISIBLE);
//
//            if(ksyMediaPlayer.getServerAddress() != null)
//                mServerIp.setText("ServerIP: "+ ksyMediaPlayer.getServerAddress());
//
//            mVideoResolution.setText("Resolution:" + ksyMediaPlayer.getVideoWidth() + "x" + ksyMediaPlayer.getVideoHeight());
//            mVideoResolution.setVisibility(View.VISIBLE);
//            mFrameRate.setVisibility(View.VISIBLE);
//            mVideoBitrate.setVisibility(View.VISIBLE);
//            mCodecType.setVisibility(View.VISIBLE);

//            ITrackInfo trackInfos[] = ksyMediaPlayer.getTrackInfo();
//            if(trackInfos != null) {
//                for(ITrackInfo infos : trackInfos)
//                {
//                    if(infos.getTrackType() == ITrackInfo.MEDIA_TRACK_TYPE_VIDEO)
//                    {
//                        IMediaFormat mediaFormat = infos.getFormat();
//                        String codecType = mediaFormat.getString(KSYMediaFormat.KEY_IJK_CODEC_LONG_NAME_UI);
//                        mFrameRate.setText("FrameRate: " + mediaFormat.getString(KSYMediaFormat.KEY_IJK_FRAME_RATE_UI));
//
//                        if(codecType.equals("hevc"))
//                            mCodecType.setText("Codec: H.265/HEVC");
//                        else if(codecType.equals("h264"))
//                            mCodecType.setText("Codec: H.264/AVC");
//                    }
//                }
//            } // end of for loop

            mStartTime = System.currentTimeMillis();

        }
    };

    private IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new IMediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(IMediaPlayer mp, int percent) {
        	Logger.i(TAG, "OnBufferingUpdateListener");
            long duration = ksyMediaPlayer.getDuration();
            long progress = duration * percent/100;
            mPlayerSeekbar.setSecondaryProgress((int)progress);
        }
    };

    private IMediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangeListener = new IMediaPlayer.OnVideoSizeChangedListener() {
        @Override
        public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sarNum, int sarDen) {
        	Logger.i(TAG, "onVideoSizeChanged111111111");
            if(mVideoWidth > 0 && mVideoHeight > 0) {
                if(width != mVideoWidth || height != mVideoHeight) {
                    mVideoWidth = mp.getVideoWidth();
                    mVideoHeight = mp.getVideoHeight();
                    Logger.i(TAG, "mVideoWidth111==="+mVideoWidth+"_mVideoHeight111=="+mVideoHeight);
                    // maybe we could call scaleVideoView here.
                    if(mVideoSurfaceView != null)
                    {
                    	setVideoDefauleScale();
                    	mVideoSurfaceView.invalidate();
//                        mVideoSurfaceView.setVideoDimension(mVideoWidth, mVideoHeight);
//                        mVideoSurfaceView.requestLayout();
                    }
                }
            }
        }
    };

    private IMediaPlayer.OnSeekCompleteListener mOnSeekCompletedListener = new IMediaPlayer.OnSeekCompleteListener() {
        @Override
        public void onSeekComplete(IMediaPlayer mp) {
//            Log.e(TAG, "onSeekComplete...............");
        	Logger.i(TAG, "OnBufferingUpdateListener");
        }
    };

    private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(IMediaPlayer mp) {
            Toast.makeText(mContext, "啊哦，播放没有了耶~~~", Toast.LENGTH_LONG).show();
//            if(!Helper.checkConnection(mContext)){
//            	ksyMediaPlayer.reload(videoUrl, false);
//        		ksyMediaPlayer.reload(videoUrl, true);
//        	}else{
        		videoPlayEnd();
//            }
        }
    };

    private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(IMediaPlayer mp, int what, int extra) {
        	Logger.i(TAG, "OnErrorListener");
//        	if(!Helper.checkConnection(mContext)){
//        		ksyMediaPlayer.reload(videoUrl, false);
//        		ksyMediaPlayer.reload(videoUrl, true);
//        	}
            switch (what)
            {
                case KSYMediaPlayer.MEDIA_ERROR_UNKNOWN:
                	Logger.i(TAG, "OnErrorListenerError111 Unknown:" + what + ",extra:" + extra);
//                    Log.e(TAG, "OnErrorListener, Error Unknown:" + what + ",extra:" + extra);
                    break;
                default:
                	Logger.i(TAG, "OnErrorListenerError333 Error:" + what + ",extra:" + extra);
//                    Log.e(TAG, "OnErrorListener, Error:" + what + ",extra:" + extra);
            }

            return false;
        }
    };

    public IMediaPlayer.OnInfoListener mOnInfoListener = new IMediaPlayer.OnInfoListener() {
        @Override
        public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
//            Log.d(TAG, "onInfo, what:"+i+",extra:"+i1);
        	Logger.i(TAG, "OnInfoListener:onInfo, what:"+i+",extra:"+i1);
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display display = getWindowManager().getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();
        
        mContext = this.getApplicationContext();
//        Window win = getWindow();
//        win.getDecorView().setPadding(0, 0, 0, 0);
//        WindowManager.LayoutParams lp = win.getAttributes();
//                lp.width = WindowManager.LayoutParams.FILL_PARENT;
//                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//                win.setGravity(Gravity.LEFT | Gravity.TOP);
////                lp.y = (int)getResources().getDimension(R.dimen.live_play_margin_top);
//                lp.y = Utils.computeImageHeightWithWidth(screenWidth,640,195);
//                win.setAttributes(lp);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.qianyi_player);       
		
        layoutPlayer_qos = (RelativeLayout) findViewById(R.id.player_qos);
        layoutPlayer_qos.setVisibility(View.GONE);
        mPlayerPanel = (LinearLayout) findViewById(R.id.player_panel);
        mPlayerStartBtn = (ImageView) findViewById(R.id.player_start);
        mPlayerSeekbar = (SeekBar) findViewById(R.id.player_seekbar);
        mPlayerPosition = (TextView) findViewById(R.id.player_time);
        mLoadText = (TextView) findViewById(R.id.loading_text);
//        mCpu = (TextView) findViewById(R.id.player_cpu);
//        mMemInfo = (TextView) findViewById(R.id.player_mem);
//        mVideoResolution = (TextView) findViewById(R.id.player_re);
//        mVideoBitrate = (TextView) findViewById(R.id.player_br);
//        mFrameRate = (TextView) findViewById(R.id.player_fr);
//        mCodecType = (TextView) findViewById(R.id.player_codec);
//        mServerIp = (TextView) findViewById(R.id.player_ip);
        
        mPlayerBtnReverse = (View)findViewById(R.id.iv_video_reverse);
        
        mPlayerBtnReverse.setOnClickListener(mPlayerBtnReverseListener);
        mPlayerStartBtn.setOnClickListener(mStartBtnListener);
        mPlayerSeekbar.setOnSeekBarChangeListener(mSeekBarListener);
        mPlayerSeekbar.setEnabled(true);

        mVideoSurfaceView = (VideoSurfaceView) findViewById(R.id.player_surface);
        mSurfaceHolder = mVideoSurfaceView.getHolder();
        mSurfaceHolder.addCallback(mSurfaceCallback);
        mVideoSurfaceView.setOnTouchListener(mTouchListener);
        mVideoSurfaceView.setKeepScreenOn(true);
        mLoadText.setVisibility(View.VISIBLE);
        
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        mHandler = new UIHandler(this);
//        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

//        mQosThread = new QosThread(activityManager, mHandler);

        videoUrl = getIntent().getStringExtra("path");

        ksyMediaPlayer = new KSYMediaPlayer.Builder(mContext).build();
        ksyMediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
        ksyMediaPlayer.setOnCompletionListener(mOnCompletionListener);
        ksyMediaPlayer.setOnPreparedListener(mOnPreparedListener);
        ksyMediaPlayer.setOnInfoListener(mOnInfoListener);
        ksyMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangeListener);
        ksyMediaPlayer.setOnErrorListener(mOnErrorListener);
        ksyMediaPlayer.setOnSeekCompleteListener(mOnSeekCompletedListener);
        ksyMediaPlayer.setScreenOnWhilePlaying(true);
        ksyMediaPlayer.setBufferTimeMax(5);
                
		Logger.i(TAG, "screenWidth=="+screenWidth + "_screenHeight=="+screenHeight);
		setVideoDefauleScale();
        // set cache dir
        // enable http local cache
//        ksyMediaPlayer.setCachedDir("/mnt/sdcard/");
		this.setFinishOnTouchOutside(false);
        try {
            ksyMediaPlayer.setDataSource(videoUrl);
            ksyMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(ksyMediaPlayer != null)
        {
            ksyMediaPlayer.pause();
            mPause = true;
        }
    }
    ZeroIntentReceiver receiver = null;
    @Override
    protected void onResume() {
        super.onResume();
        
        if(ksyMediaPlayer != null)
        {
            ksyMediaPlayer.start();
            mPause = false;
        }
        IntentFilter filter = new IntentFilter(ZeroIntentReceiver.Broad_Action);
        receiver = new ZeroIntentReceiver();
        registerReceiver(receiver, filter);
    }

    @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unregisterReceiver(receiver);
	}
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            videoPlayEnd();
        }
        return true;
//        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int getChangingConfigurations() {
        return super.getChangingConfigurations();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
//        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//			setVideoDefauleScaleLandScape();
//		}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//			setVideoDefauleScale();
//			View decorView = getWindow().getDecorView();
//			int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;  
//			decorView.setSystemUiVisibility(uiOptions);  
//		}
    }

    private void scaleVideoView()
    {
        if(ksyMediaPlayer == null || ksyMediaPlayer.getVideoHeight() <= 0 || mVideoSurfaceView == null)
            return;

        WindowManager wm = this.getWindowManager();
        int sw = wm.getDefaultDisplay().getWidth();
        int sh = wm.getDefaultDisplay().getHeight();
        int videoWidth = mVideoWidth;
        int videoHeight = mVideoHeight;
        int visibleWidth = 0;
        int visibleHeight = 0;

        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            visibleWidth = sw > sh ? sh : sw;
            visibleHeight = (int) Math.ceil(visibleWidth * videoHeight / videoWidth);
        }
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

            if(videoHeight*sw > videoWidth*sh)
            {
                visibleHeight = sh;
                visibleWidth = (int) Math.ceil(videoWidth * visibleHeight / videoHeight);
            }
            else
            {
                visibleWidth = sw;
                visibleHeight = (int) Math.ceil(visibleWidth * videoHeight / videoWidth);
            }
        }

        LayoutParams lp = mVideoSurfaceView.getLayoutParams();
        lp.width = visibleWidth;
        lp.height = visibleHeight;
        mVideoSurfaceView.setLayoutParams(lp);

        mVideoSurfaceView.invalidate();
    }

    // Maybe we could support gesture detect
    private void dealTouchEvent(View view, MotionEvent event) {
        mPlayerPanelShow = !mPlayerPanelShow;

        if(mPlayerPanelShow) {
            mPlayerPanel.setVisibility(View.VISIBLE);

            Message msg = new Message();
            msg.what = HIDDEN_SEEKBAR;
            mHandler.sendMessageDelayed(msg, 3000);
        }else{
            mPlayerPanel.setVisibility(View.GONE);
            mHandler.removeMessages(HIDDEN_SEEKBAR);
        }
    }

    public int setVideoProgress(int currentProgress) {

        if(ksyMediaPlayer == null)
            return -1;

        long time = currentProgress > 0 ? currentProgress : ksyMediaPlayer.getCurrentPosition();
        long length = ksyMediaPlayer.getDuration();

        // Update all view elements
        mPlayerSeekbar.setMax((int)length);
        mPlayerSeekbar.setProgress((int)time);

        if(time >= 0)
        {
            String progress = Strings.millisToString(time) + "/" + Strings.millisToString(length);
            mPlayerPosition.setText(progress);
        }

        Message msg = new Message();
        msg.what = UPDATE_SEEKBAR;

        if(mHandler != null)
            mHandler.sendMessageDelayed(msg, 1000);
        return (int)time;
    }

    private void updateQosInfo(QosObject obj) {
//        mCpu.setText("Cpu usage:"+obj.cpuUsage);
//        mMemInfo.setText("Memory:" + obj.pss + " KB");
//
//        if(ksyMediaPlayer != null)
//        {
//            long bits = ksyMediaPlayer.getDecodedDataSize() * 8 / (mPause ? mPauseStartTime - mPausedTime - mStartTime : System.currentTimeMillis() - mPausedTime - mStartTime);
//            mVideoBitrate.setText("Bitrate: " + bits + " kb/s");
//        }

    }

    private String md5(String string)
    {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }

        return hex.toString();
    }

    private void videoPlayEnd() {
//    	if(getResources().getConfiguration().orientation == 1){
    		if(ksyMediaPlayer != null)
            {
                ksyMediaPlayer.release();
                ksyMediaPlayer = null;
            }

//            if(mQosThread != null) {
//                mQosThread.stopThread();
//                mQosThread = null;
//            }

            mHandler = null;

            finish();
//		}else{
//			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//		}
        
    }

    private View.OnClickListener mStartBtnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPause = !mPause;
            if(mPause) {
//                mPlayerStartBtn.setBackgroundResource(R.drawable.qyvideo_pause_btn);
            	mPlayerStartBtn.setImageResource(R.drawable.btn_play_video);
                ksyMediaPlayer.pause();
                mPauseStartTime = System.currentTimeMillis();
            }else {
//                mPlayerStartBtn.setBackgroundResource(R.drawable.qyvideo_start_btn);
            	mPlayerStartBtn.setImageResource(R.drawable.btn_play_video_pause);
                ksyMediaPlayer.start();
                mPausedTime += System.currentTimeMillis() - mPauseStartTime;
                mPauseStartTime = 0;
            }
        }
    };
    
    private View.OnClickListener mPlayerBtnReverseListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//        	if(getResources().getConfiguration().orientation == 1){
//				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//			}else{
//				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//			}
        	videoPlayEnd();
        }
    };
    
    private int mVideoProgress = 0;
    private SeekBar.OnSeekBarChangeListener mSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(fromUser) {
                mVideoProgress = progress;
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            ksyMediaPlayer.seekTo(mVideoProgress);
            setVideoProgress(mVideoProgress);
        }
    };

    private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            dealTouchEvent(v, event);
            return false;
        }
    };

    private final SurfaceHolder.Callback mSurfaceCallback = new Callback() {
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            if(ksyMediaPlayer != null) {
                final Surface newSurface = holder.getSurface();
                ksyMediaPlayer.setDisplay(holder);
                ksyMediaPlayer.setScreenOnWhilePlaying(true);
                if (mSurface != newSurface) {
                    mSurface = newSurface;
                    ksyMediaPlayer.setSurface(mSurface);
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");
            if(ksyMediaPlayer != null) {
                mSurface = null;
            }
        }
    };

    private class UIHandler extends Handler {

        VideoPlayerActivityJin mActivtiy;

        public UIHandler(VideoPlayerActivityJin activty) {
            mActivtiy = activty;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_SEEKBAR:
                    if(mActivtiy != null)
                        mActivtiy.setVideoProgress(0);
                    break;
                case HIDDEN_SEEKBAR:
                    if(mActivtiy != null)
                    {
                        mActivtiy.mPlayerPanelShow = false;
                        mActivtiy.mPlayerPanel.setVisibility(View.GONE);
                    }
                    break;
                case UPDATE_QOS:
                    if(mActivtiy != null && msg.obj instanceof QosObject) {
                        mActivtiy.updateQosInfo((QosObject)msg.obj);
                    }
                    break;
            }
        }
    }
    
    public class ZeroIntentReceiver extends BroadcastReceiver{

        public final String TAG = "ZeroIntentReceiver";
        public static final String Broad_Action = "android.net.conn.CONNECTIVITY_CHANGE";//网络状态改变广播
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Logger.i(TAG, "网络发生变化 了哦");
        	
            if(!Helper.checkConnection(context)){
//                Logger.i(TAG, "网络发生变化 _关网了");
                return;
            }else{
            	ksyMediaPlayer.reload(videoUrl, true);
            }

        }

    }
}
